package com.example.jedis_pool.test;

import com.example.jedis_pool.jedis_config.Config;
import redis.clients.jedis.Jedis;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class testHash {

    public static void main(String[] args) {

        testHash.setKeyValue();
        testHash.getKeyValue();
        //关闭连接
        jedis.close();
    }
    //hash类型中的feild是无序的，并且排重

    //获取连接
    private static Jedis jedis= Config.JedisPool().getResource();

    public static void setKeyValue(){
        //更改数据库
        jedis.select(2);

        jedis.hset("hash","hash-field-0","hash-value-0");
        Map map=new HashMap();
        map.put("hash-field-1","hash-value-1");
        map.put("hash-field-2","hash-value-2");
        map.put("hash-field-3","hash-value-3");
        map.put("hash-field-4","hash-value-4");
        map.put("hash-field-5","hash-value-5");
        String hash = jedis.hmset("hash", map);
        System.out.println(hash);
        jedis.hsetnx("hash","hash-field-6","6");
        jedis.hincrBy("hash","hash-field-6",1);


    }


    public static void getKeyValue(){

        String s = jedis.hget("hash", "hash-field-3");
        System.out.println(s);

        Set<String> list1 = jedis.hkeys("hash");//feild是无序的
        List<String> list2 = jedis.hvals("hash");//values是有序的。
        System.out.println("hash表中的所有键："+list1+"，所有值："+list2);

        System.out.println("hash-field-6的值："+jedis.hget("hash","hash-field-6"));

        System.out.println(jedis.hexists("hash","hash-field"));

    }

}
